<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../../css/reset.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/add.css"/>
</head>
<body>
<div id="app">
    <div class="header">
        <span>修改快递信息</span>
    </div>
    <div class="content">
        <table>
            <tr>
                <td class="text-right">运单号</td>
                <td class="content_right"><input class="input inline-input" placeholder="请输入运单号码" id="number"> <span
                        id="find" class="btn btn-info">立即查找</span></td>
            </tr>
        </table>
    </div>


    <div class="header">
        <span>查找信息如下</span>
    </div>
    <div class="content">
        <table>
            <tr>
                <td class="text-right">快递单号</td>
                <td class="content_right"><input id="newnumber" class="input" placeholder="请输入姓名"></td>
            </tr>
            <tr>
                <td class="text-right">快递公司</td>
                <td class="content_right"><input id="newcompany" class="input" placeholder="请输入姓名"></td>
            </tr>
            <tr>
                <td class="text-right">收货人姓名</td>
                <td class="content_right"><input id="newusername" class="input" placeholder="请输入姓名"></td>
            </tr>
            <tr>
                <td class="text-right">手机号码</td>
                <td class="content_right"><input id="newuserphone" class="input" placeholder="请输入姓名"></td>
            </tr>
            <tr>
                <td class="text-right">快递状态</td>
                <td class="content_right"><input type="radio" class="status" name="status" value="1">已签收 <input
                        class="status" type="radio" name="status" value="0">未签收
                </td>
            </tr>
            <tr>
                <td></td>
                <td class="content_right"><span class="btn btn-info" id="update">立即修改</span> <span class="btn">重置</span>
                </td>
            </tr>
        </table>
    </div>
</div>
</body>
<script src="/qrcode/jquery2.1.4.js"></script>
<script src="/layer/layer.js"></script>


<script>
    function isNumber() {
        var number2 = $("input:eq(1)").val();
        if (number2 == "") {
            layer.tips("单号不能为空", "#newnumber", {tipsMore: true, tips: 1});
            return false;
        } else if (/^\d{5,}$/.test(number2) == false) {
            layer.tips("单号的长度不能少于5位", "#newnumber", {tipsMore: true, tips: 1});
            return false;
        }
        return true;
    }

    function isUserName() {
        var userName = $("input:eq(3)").val();
        if (userName == "") {
            layer.tips("姓名不能为空", "#newusername", {tipsMore: true, tips: 1});
            return false;
        } else if (/^[\u4E00-\u9FA5A-Za-z0-9_]+$/.test(userName) == false) {      //英文，中文，数字，下划线
            layer.tips("姓名只支持英文、中文、数字和下划线", "#newusername", {tipsMore: true, tips: 1});
            return false;
        }
        return true;
    }

    function isUserPhone() {
        var userPhone = $("input:eq(4)").val();
        if (userPhone == "") {
            layer.tips("电话不能为空", "#newuserphone", {tipsMore: true, tips: 1});
            return false;
        } else if (/^((0\d{2,3}-\d{7,8})|(1[345789]\d{9}))$/.test(userPhone) == false) {      //英文，中文，数字，下划线
            layer.tips("电话格式不合法", "#newuserphone", {tipsMore: true, tips: 1});
            return false;
        }
        return true;
    }
</script>


<script>
    $(".header:eq(1),.content:eq(1)").hide();
    var expressId = null;
    var g_userPhone = null;
    var g_status = null;
    $(function () {

        $("input:eq(1)").blur(function () {
            isNumber();
        });

        $("input:eq(3)").blur(function () {
            isUserName();
        });

        $("input:eq(4)").blur(function () {
            isUserPhone();
        });


        $("#find").click(function () {
            var windowId = layer.load();
            var number1 = document.getElementById("number").value;   //获取查找的单号
            if (number1.length > 0) {
                $.getJSON("/express/find.do", {number: number1}, function (data) {
                    layer.close(windowId);
                    layer.msg(data.result);
                    if (data.status == 0) {			//如果查询成功
                        $(".header:eq(1),.content:eq(1)").fadeIn(1000);   //设置第二个有header 这个class的标签，慢慢的显示出来
                        expressId = data.data.id;
                        g_userPhone = data.data.userPhone;
                        $("input:eq(1)").val(data.data.number);
                        $("input:eq(2)").val(data.data.company);
                        $("input:eq(3)").val(data.data.userName);
                        $("input:eq(4)").val(data.data.userPhone);

                        if (data.data.status == 0) {
                            //未签收
                            $(".status:eq(1)").prop("checked", true);
                            g_status = $(".status:checked").val();
                        } else {
                            //已签收
                            $(".status:eq(0)").prop("checked", true);
                            g_status = $(".status:checked").val();

                        }


                    }
                });
            } else {
                layer.close(windowId);
                layer.msg("<span style='color:black'>" + "查询的单号不能为空" + "</span>", {icon: 7, shade: 0.01});
            }

        });


            $("#update").click(function () {

                var bool = isNumber() && isUserName() && isUserPhone();
                if (bool) {
                    var windowId = layer.load();
                    var number = $("input:eq(1)").val();
                    var company = $("input:eq(2)").val();
                    var username = $("input:eq(3)").val();
                    var userPhone = $("input:eq(4)").val();

                    var status = $(".status:checked").val();    //获取选中的那个单元框的值 0 或者 1  1表示已签收
                    var express = {
                        id: expressId,
                        number: number,
                        company: company,
                        userName: username,
                        status: status
                    };
                    if (userPhone != g_userPhone) {
                        express.userPhone = userPhone;
                        if (status != g_status) {
                            layer.msg("修改手机号码同时不能修改快递状态");
                            layer.close(windowId);
                            return false;
                        }
                    } else {
                        express.userPhone = "";
                    }
                    $.getJSON("/express/update.do", express, function (data) {
                        layer.close(windowId);
                        layer.msg(data.result)

                        if (data.status == 0) {
                            //修改成功的
                            $(".header:eq(1),.content:eq(1)").hide();
                        }
                    })
                }
            });



    });

</script>

</html>
